﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Smartec.Accounts.Camel.AMS.Model.Entities;

namespace Smartec.Accounts.Camel.AMS.Model.Repository
{
//    class DBAuditRepository
//    {
//    }
//}
    internal static class TopicRepository
    {

        internal static void AddTopic(Topic topic)
        {
            AMSObjectContextHelper.CurrentObjectContext.Topics.AddObject(topic);
        }
        internal static void Save()
        {
            AMSObjectContextHelper.CurrentObjectContext.SaveChanges();
        }
        //
        // Query Methods

        //public static IEnumerable<DBAuditDetail> FindAllDBAuditDetails(DateTime? dateFrom, DateTime? dateUntil, string tableName, int entityId, string userName, string action)
        //{
        //    if (dateFrom != null)
        //        dateFrom = dateFrom.Value.Date;

        //    if (dateUntil != null)
        //        dateUntil = dateUntil.Value.Date.AddDays(1);

        //    //return AMSObjectContextHelper.CurrentObjectContext.DBAuditDetails;
        //    return AMSObjectContextHelper.CurrentObjectContext.DBAuditDetails
        //        .AsEnumerable().Where(o => (dateFrom == null || o.DBAudit.RevisionStamp >= dateFrom) && (dateUntil == null || o.DBAudit.RevisionStamp < dateUntil) && (string.IsNullOrEmpty(tableName) || o.DBAudit.TableName.ToLower() == tableName.ToLower()) && (entityId == -1 || o.DBAudit.MasterId == entityId) && (string.IsNullOrEmpty(userName) || o.DBAudit.UserName.ToLower() == userName.ToLower()) && (string.IsNullOrEmpty(action) || o.Action.ToLower() == action.ToLower()))
        //        .OrderByDescending(o => o.RevisionStamp)
        //        .Select(o => new DBAuditDetail { ID = o.ID, Audit = o.Audit, RevisionStamp = o.RevisionStamp, MasterName = o.MasterName, MasterEntity = o.MasterEntity, TableName = o.TableName, UserName = o.UserName, Action = o.Action, Changes = o.Changes, EntityName = o.EntityName, AuditId = o.AuditId });
        //}

        //public static DBAuditDetail GetDBAuditDetail(int id)
        //{
        //    return AMSObjectContextHelper.CurrentObjectContext.DBAuditDetails.SingleOrDefault(d => d.ID == id);
        //}


        internal static List<Topic> All()
        {
          return  AMSObjectContextHelper.CurrentObjectContext.Topics.ToList();
        }

        internal static void DeleteTopicById(int id)
        {
            var topic = AMSObjectContextHelper.CurrentObjectContext.Topics.FirstOrDefault(t => t.ID == id);
            if (topic != null)
            {
                AMSObjectContextHelper.CurrentObjectContext.Topics.DeleteObject(topic);
            }
        }
    }
}
